/*
			Author: Ozcan Degirmenci (Boot)
			E-Mail: odegirmenci@hotmail.com
			Date:	4 Jan 2005
*/

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace Ling.ZhControls
{
	/// <summary>
	///  Simple Tab Page item.
	/// </summary>
	[ToolboxItem(false)]
	public class SimpleTabPage : System.Windows.Forms.Panel
	{
		SimpleTabPageCollection			_ParentCollection		= null				;
		int								_ImageIndex				= -1				;
		bool							_TabVisible				= true				;

		Rectangle _TabRectangle = Rectangle.Empty;
		/// <summary>
		///  Initialize a new instance of this class
		/// </summary>
		public SimpleTabPage()
		{
			SetStyle(ControlStyles.SupportsTransparentBackColor |
				ControlStyles.UserPaint |
				ControlStyles.ResizeRedraw, true);
			
		}

		internal void FireActivatedEvent(bool activated)
		{
			if (activated)
				OnActivatedTab();
			else
				OnDeactivatedTab();
		}

//		protected override void OnHandleCreated(EventArgs e)
//		{
//			base.OnHandleCreated (e);
//			int a = StaticPaint.SetLayeredWindowAttributes (
//				new System.Runtime.InteropServices.HandleRef (this, this.Handle),
//				0, (byte)(0.1 * 255), 2);
//
//			int v = StaticPaint.GetLastError ();
//			
//			string c = StaticPaint.GetMessage (v, "kernel32.dll");
//			int k = 8;
//		}
//
//		static int WS_EX_LAYERED			= 0x00080000;
//		protected override CreateParams CreateParams
//		{
//			get
//			{
//				base.CreateParams.ExStyle |= WS_EX_LAYERED;
//				return base.CreateParams;
//			}
//		}

		public const int WM_VSCROLL                = 0x0115;

		protected override void WndProc(ref Message m)
		{
			base.WndProc (ref m);
			if (m.Msg == WM_VSCROLL)
			{
				Invalidate();
			}
		}

		protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			if (TabControl == null || (TabControl.DrawMode == TabDrawMode.Normal))
				base.OnPaintBackground(pevent);
			else
			{
				LinearGradientBrush br = new LinearGradientBrush(TabControl.ClientRectangle,
					TabControl.SelectedTabColor, TabControl.TabGradientColor,
					LinearGradientMode.Vertical);
				pevent.Graphics.FillRectangle(br, ClientRectangle);
				br.Dispose();
			}
		}


		/// <summary>
		///  Gets the index of this item
		/// </summary>
		[Browsable (false)]
		public int Index
		{
			get {
                if (ParentCollection == null)
                    return -1;
                return ParentCollection.IndexOf (this);
			}
		}

		/// <summary>
		///  Overrides to text property.
		///  Because in Panel text properties EDitorBrowsable state changed to the 
		///  Never. So to make it visible we override to it
		/// </summary>
		[Browsable(true),
		EditorBrowsable(EditorBrowsableState.Always)]
		public override string Text
		{
			get {return base.Text;}
			set {
				if (value == base.Text)
					return;
				base.Text = value;
				if (TabControl != null)
					TabControl.DrawControl ();
			}
		}


		/// <summary>
		///  Tab control which hosts this 
		/// </summary>
		[Browsable (false)]
		public SimpleTabControl TabControl
		{
			get
			{
				if (ParentCollection == null)

					return null;
				return ParentCollection.Parent;
			}
		}

		[Browsable(false)]
		public virtual ImageList ImageList
		{
			get
			{
				if(TabControl != null)
					return TabControl.ImageList;
				return null;
			}
		}


		/// <summary>
		///  Tab Rectangle
		/// </summary>
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden),
		Browsable(false), EditorBrowsable (EditorBrowsableState.Advanced)]
		internal Rectangle TabRectangle
		{
			get {return _TabRectangle;}
			set {_TabRectangle = value;}
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				
			}
			base.Dispose( disposing );
		}

		/// <summary>
		///  Parent container colection
		/// </summary>
		[Browsable (false), 
		DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		internal SimpleTabPageCollection ParentCollection
		{
			get {return _ParentCollection;}
			set {_ParentCollection = value;}
		}

		[Editor ("System.Windows.Forms.Design.ImageIndexEditor",
			 "System.Drawing.Design.UITypeEditor"), 
		TypeConverter(typeof(System.Windows.Forms.ImageIndexConverter)),
		Description("Gets or sets the image index"),
		DefaultValue(-1), Category("Apperance")]
		public int ImageIndex
		{
			get {return _ImageIndex;}
			set {
				if (_ImageIndex == value)
					return;
				_ImageIndex = value;
				if (TabControl != null)
					TabControl.DrawControl ();
			}
		}

		private static readonly object activatedTab = new object();
		/// <summary>
		///  Occurs when this tab activated
		/// </summary>
		public event EventHandler ActivatedTab
		{
			add { Events.AddHandler(activatedTab, value); }
			remove { Events.RemoveHandler(activatedTab, value); }
		}

		protected virtual void OnActivatedTab()
		{
			EventHandler h = Events[activatedTab] as EventHandler;
			if (h != null)
				h(this, EventArgs.Empty);
		}

		private static readonly object deactivatedTab = new object();
		/// <summary>
		///  Occurs when this tab activated
		/// </summary>
		public event EventHandler DeactivatedTab
		{
			add { Events.AddHandler(deactivatedTab, value); }
			remove { Events.RemoveHandler(deactivatedTab, value); }
		}

		protected virtual void OnDeactivatedTab()
		{
			EventHandler h = Events[deactivatedTab] as EventHandler;
			if (h != null)
				h(this, EventArgs.Empty);
		}

		// browsable false overrides
		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)*/]
		public override AnchorStyles Anchor
		{
			get	{return base.Anchor;}
			set	{base.Anchor = value;}
		}

		[Browsable (false), EditorBrowsable (EditorBrowsableState.Never)]
		public override DockStyle Dock
		{
			get	{return base.Dock;}
			set {base.Dock = value;	}
		}

		[Browsable (false), EditorBrowsable (EditorBrowsableState.Never)]
		public override RightToLeft RightToLeft
		{
			get	{return base.RightToLeft;}
			set {base.RightToLeft = value;}
		}

        [Browsable (false), EditorBrowsable (EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)*/]
        public new Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)*/]
		public new Point Location
        {
            get { return base.Location; }
            set { base.Location = value; }
        }

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/]
		public new bool TabStop
		{
			get { return base.TabStop; }
			set { base.TabStop = value; }
		}

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/
			]
		public new int TabIndex
		{
			get { return base.TabIndex; }
			set { base.TabIndex = value; }
		}

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
		public bool IsSelected
		{
			get {
				if (TabControl == null)
					return false;
				return (TabControl.SelectedItem == this);
			}
		}

#if WHIDBEY
		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/
			]
		public override Size MaximumSize
		{
			get { return base.MaximumSize; }
			set { base.MaximumSize = value; }
		}
#endif

#if WHIDBEY
		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/
			]
		public override Size MinimumSize
		{
			get { return base.MinimumSize; }
			set { base.MinimumSize = value; }
		}
#endif

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/
			]
		public override Color BackColor
		{
			get { return base.BackColor; }
			set { base.BackColor = value; }
		}

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/
			]
		public override Image BackgroundImage
		{
			get { return base.BackgroundImage; }
			set { base.BackgroundImage = value; }
		}

#if WHIDBEY
		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/
			]
		public override ImageLayout BackgroundImageLayout
		{
			get { return base.BackgroundImageLayout; }
			set { base.BackgroundImageLayout = value; }
		}
#endif

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)/*,
		DesignerSerializationVisibility 
		(DesignerSerializationVisibility.Hidden)*/
			]
		public new BorderStyle BorderStyle
		{
			get { return base.BorderStyle; }
			set { base.BorderStyle = value; }
		}

		[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
		public new bool Visible
		{
			get {return base.Visible;}
			set {base.Visible = value;}
		}

		[DefaultValue(true),
		Category("Appearance"),
		Description("Indicates the visible state of this tab page")]
		public bool TabVisible
		{
			get {return _TabVisible;}
			set {if (_TabVisible == value)
					 return;
				
				_TabVisible = value;
				if (TabControl != null)
				{
					TabControl.Tabs.Calculated = false;
					TabControl.DrawControl ();

					if (!value && TabControl.SelectedItem == this && !base.DesignMode)
					{
						base.Visible = false;
						TabControl.SelectedItem = null;
					}
				}
			}
		}

		internal bool IsNeedToDraw
		{
			get {
				if (TabVisible)
					return true;
				if (base.DesignMode)
					return true;
				return false;
			}
		}
	}
}
